/*
 * twist.cpp
 *
 * Copyright 2013 Stefan Fruhner
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */
#include <mylibs/myexception.hpp>
#include <mylibs/CardiacMesh.hpp>
#include <mylibs/myinifiles.hpp>
#include <mylibs/cmdline.hpp>
#include <mylibs/myio.hpp>
#include <iostream>
#include <fstream>


using namespace std;
using namespace mylibs;

bool verbose = false;

//void set_regions(double endo_mcell, double mcell_epi, CardiacMesh &mesh) {

//	/*if (endo_mcell > mcell_epi || endo_mcell == mcell_epi) throw myexception("Transition Endo to M-Cell must be smaller than M-Cell to Epi and cannot be equal");
//	if (endo_mcell >= 1 || endo_mcell <=0) throw myexception("Position must be element of [0,1]");
//	if (mcell_epi >= 1 || mcell_epi <=0) throw myexception("Position must be element of [0,1]");*/

////	Face *faces = &(mesh.f[0]);

//	for (size_t i = 0; i < mesh.intra_elements(); i++){


//		const size_t &idx = mesh.elem_i2e(i);
//		//ENDO
//		if (mesh.transmural_position(i) > endo_mcell){
//			mesh.f[idx].attribute = 23;
//		}
//		//EPI
//		else if (mesh.transmural_position(i) < mcell_epi){
//			mesh.f[idx].attribute = 21;
//		}
//		//M-Cell
//		else {
//			mesh.f[idx].attribute = 22;
//		}
//	}
//	return;
//}

int main(int argc, char **argv){
	myIniFiles ini(argc, argv);

	ini.set_info("Set distinct regions to be part bath. CARP assumes a \
node to be bath if the longitudinal fiber direction has zero length.");
	ini.register_param("mesh", "m", "Mesh basename without extension.");
	ini.register_param("region", "r", "List of region IDs that is to be converted into bath.");

	ini.check(2);

	mystring meshfile;
	mystring output 	= mystring();

	meshfile = ini.read("mesh", mystring());
	vector<int> region;
	ini.read("region", region);

	if (meshfile.empty()) 	ini.print_help("mesh");
	if (region.size() == 0) ini.print_help("region");


	CardiacMesh mesh(meshfile, true); 	// load the mesh
	mesh.read_lon(); 					// load the fiber structure

//	// Get transmural positions
//	mesh.read_transmural_pos();

//	set_regions(endo_mcell, mcell_epi, mesh);



	set<int> bathregions(region.begin(), region.end());
	for (size_t i = 0; i < mesh.elements(); i++){
		Face &f = mesh.f[i];
		if (bathregions.find(f.attribute) != bathregions.end()){
			mesh.elem2bath(i); // define this element to be bath
		}
	}

	mystring src(mesh.GetBasename().file_new_ext(".lon"));
	mystring tgt(mesh.GetBasename().file_new_ext(".lon.orig"));
	IO::cp(src, tgt);
	mesh.save_lon();

	return 0;
}
